<?php

namespace App\Utils;


class ProjectUtil
{
    /**
     * 获取随机字符串
     * @param int $length
     * @return string
     */
    public static function generateRandomString(int $length = 64): string
    {
        $characters       = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $charactersLength = strlen($characters);
        $randomString     = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[random_int(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    /**
     * 生成n位包含$string的随机数
     * @param int $length
     * @param string $chars
     * @return string
     */
    public static function createRandomStringKey(int $length = 32, string $chars = "abcdefghilkmnopqrstuvwxyz0123456789"): string
    {
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    /**
     * 获取毫秒时间戳
     * @return float
     */
    public static function getMillisecondTime(): float
    {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }
}